﻿<Window x:Class="Demo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="640" Width="480"
    xmlns:controls="clr-namespace:DevIsMagic.Wpf.Controls;assembly=DevIsMagic.Wpf.Controls"    
    Closing="Window_Closing"    >
    <Window.Resources>
        <ContextMenu
            x:Key="ColumnsSelector"
            ItemsSource="{Binding Path=Columns}"               
            >
            <ContextMenu.ItemTemplate>
                <DataTemplate>
                    <MenuItem Header="{Binding Path=Header}" IsCheckable="True" IsChecked="{Binding Path=Visible, Mode=TwoWay}" />
                </DataTemplate>
            </ContextMenu.ItemTemplate>
        </ContextMenu>

    </Window.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="100" />
        </Grid.RowDefinitions>
        <ListView
            ItemsSource="{Binding Path=Contacts}" 
            ContextMenu="{StaticResource ColumnsSelector}"
            x:Name="lst"
            >            
            <ListView.View>
                <GridView x:Name="ContactsGridView" >
                </GridView>
            </ListView.View>
        </ListView>
        <!-- This is not working (WithBinding) : -->
        <!--<controls:GridViewColumnCollectionProxy
            Source="{Binding Path=Columns}"
            Target="{Binding Path=Columns, ElementName=ContactsGridView}"
            WidthBinding="{Binding Path=Width, Mode=TwoWay}" 
            >-->
        <controls:GridViewColumnCollectionProxy
            Source="{Binding Path=Columns}"
            Target="{Binding Path=Columns, ElementName=ContactsGridView}"
            >

        </controls:GridViewColumnCollectionProxy>
        <StackPanel Grid.Row="1">
            <TextBlock Text="Columns source :" />
            <ListView ItemsSource="{Binding Path=Columns}">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Header" DisplayMemberBinding="{Binding Path=Header}" />
                        <GridViewColumn Header="MemberName" DisplayMemberBinding="{Binding Path=MemberName}" />
                        <GridViewColumn Header="Visible" DisplayMemberBinding="{Binding Path=Visible}" />
                        <GridViewColumn Header="Width" DisplayMemberBinding="{Binding Path=Width}" />
                    </GridView>
                </ListView.View>
            </ListView>
        </StackPanel>
    </Grid>
</Window>
